package gameObjects;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.state.RenderState;
import com.jme.system.DisplaySystem;
import audio.SFX;

public class UpdateKeys extends Node {
	private static final long 		serialVersionUID = 2704101244L;
	private Text 			key;
    private StringBuffer	strBuf;
    private String			strKeys;
    private int 			height = DisplaySystem.getDisplaySystem().getHeight();
    private SFX				plin = new SFX("sound/off2.ogg", false, 0.2f, 0.2f, false);
    private float			ratio = 1.04f;
    private float 			posX =0;
    private int 			keysTot = 5;
    private boolean			isHuman;
    
	public UpdateKeys(boolean isHumanPlayer)
	{
		
		super("KEYS");
		posX = DisplaySystem.getDisplaySystem().getWidth() /ratio;
		isHuman=isHumanPlayer;
		NumberFormat formatter = new DecimalFormat("00"); 
		if (isHuman)
			strKeys = formatter.format(keysTot);
		else
			strKeys = "";
		
		strBuf=new StringBuffer(10);
		strBuf.setLength(0);
		strBuf.append(strKeys);
		
		key = new Text();
		key = Text.createDefaultTextLabel( "KEY label" );
		key.setTextColor(ColorRGBA.red.clone());
        key.setCullHint(CullHint.Never);
        key.setTextureCombineMode(TextureCombineMode.Replace);
		key.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		key.setLightCombineMode(Spatial.LightCombineMode.Off);
		key.setLocalTranslation(new Vector3f(posX, height - 20, 0));
		key.print(strBuf);
		
		this.setName("COINS_NODE");
        this.setRenderState(this.getRenderState(RenderState.StateType.Blend));
        this.setRenderState(this.getRenderState(RenderState.StateType.Texture));
        this.attachChild(key);
        this.setCullHint(CullHint.Never);
	}

	public int update()
	{
		if (isHuman)
		{
			NumberFormat formatter = new DecimalFormat("00"); 
			keysTot--;
			strKeys = formatter.format(keysTot);   
			key.setLocalTranslation(new Vector3f(posX, height-20, 0));
	        strBuf.setLength(0);
	        strBuf.append(strKeys);
	        key.print(strKeys);
	        play();
		}
		return keysTot;
	}
	
	private void play()
	{
		plin.Play();
	}

}
